package cn.lcc.basic.service.impl;


import cn.lcc.basic.mapper.BaseMapper;
import cn.lcc.basic.query.BaseQuery;
import cn.lcc.basic.service.IBaseService;
import cn.lcc.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;


    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        baseMapper.del(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        //1.查询总数量 - 前端计算分页栏
        Integer totals = baseMapper.queryCount(baseQuery);
        if(totals==null){//没有数据
            return new PageList<>();
        }

        //2.查询当前页的分页数据 - 前端展示当前页的数据
        List<T> data = baseMapper.queryData(baseQuery);

        return new PageList<>(totals,data);
    }

    @Override
    public void patchDel(Long[] ids) {
        baseMapper.patchDel(ids);
    }
}